# ============================================================
# by howlink *
# Filename       : addListener.sh
#
# Description:  create  listener entres  
# exmple: ./addTnsname.sh orcl 192.168.2.228 192.168.2.229
# =============================================================
#! /bin/sh

if [ $# -lt 1 ]; then
echo 'parameters is less 1'
   exit 1
fi

sid=$1

orahome=`cat /opt/howlink/lb-agent/application.properties | grep "^agent.oracle-home" | awk -F'=' '{ print $2 }' | sed 's/ //g'`
if [ -z "$orahome" ];then
    orahome=`grep -wi "$sid:"  $otab | grep "^$sid" | head -n1 | cut -f2 -d":"`
fi

listener=$orahome/network/admin/listener.ora

orauser=`cat /opt/howlink/lb-agent/application.properties | grep "^agent.oracle-user" | awk -F'=' '{ print $2 }' | sed 's/ //g'`
if [ -z "$orauser" ];then
    orauser=`ps -ef | grep ora_pmon | grep -v grep | head -n1 | sed -e 's/^[ \t]*//' | cut -d" " -f1`
fi
if [ -z "$orauser" ];then
    orauser=oracle
fi

if [ ! -e "$listener" ];then
  touch "$listener"
  chown -R $orauser "$listener"
fi

ret=`cat $listener |grep hlstd1`
if [ "$ret" != "" ]; then
  echo 'source listener is already in listener.ora'
  exit 0
fi
echo "
SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = hlstd1)
      (ORACLE_HOME = $orahome)
      (SID_NAME = $sid)
    )
  )
" >>$listener
